﻿using System.Collections.Concurrent;
using System.Text;

namespace MyFileHelp
{
    /// <summary>
    /// 文件帮助类
    /// </summary>
    public class MyFileHelper
    {
        /// <summary>
        /// 文件存在重命名
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string CheckedFilePath(string filePath, int index)
        {
            string result = filePath;
            if (File.Exists(filePath))
            {
                string path = Path.GetDirectoryName(filePath);
                string name = Path.GetFileName(filePath);
                string[] strArray = name.Split('.');
                index++;
                name = $"{strArray[0]}-{index}.{strArray[1]}";
                string newPath = Path.Combine(path, name);
                result = CheckedFilePath(newPath, newPath, index);
            }
            return result;
        }

        static string CheckedFilePath(string filePath, string newFilePath, int index)
        {
            string result = newFilePath;
            if (File.Exists(newFilePath))
            {
                string path = Path.GetDirectoryName(filePath);
                string name = Path.GetFileName(filePath);
                string[] strArray = name.Split('.');
                index++;
                name = $"{strArray[0]}-{index}.{strArray[1]}";
                string newPath = Path.Combine(path, name);
                result = CheckedFilePath(newPath, index);
            }
            return result;
        }

        /// <summary>
        /// 查找路径下所有文件
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <returns>文件路径集合</returns>
        public static List<string> SearchFilePaths(string folderPath)
        {
            try
            {
                List<string> result = new List<string>();
                if (!Directory.Exists(folderPath))
                {
                    return result;
                }
                foreach (string item in Directory.GetFiles(folderPath))
                {
                    result.Add(item);
                }
                return result;
            }
            catch
            {
            }
            return null;
        }

        /// <summary>
        /// 获取路径下的文件
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="FileQueue"></param>
        /// <param name="FileCountLength"></param>
        /// <param name="extensions"></param>
        public static void GetFiles(string folderPath, ref ConcurrentQueue<string> FileQueue, ref long FileCountLength, List<string> extensions)
        {
            try
            {
                if (string.IsNullOrEmpty(folderPath))
                    return;
                DirectoryInfo directory = new DirectoryInfo(folderPath);
                if (directory.Exists)
                {
                    foreach (FileInfo info in directory.GetFiles())
                    {
                        if (extensions.Contains(info.Extension.ToLower()))
                        {
                            FileCountLength += info.Length;
                            if (!FileQueue.Any(p => p == info.FullName))
                            {
                                FileQueue.Enqueue(info.FullName);
                            }
                        }
                    }
                    foreach (DirectoryInfo info in directory.GetDirectories())
                    {
                        GetFiles(info.FullName, ref FileQueue, ref FileCountLength, extensions);
                    }
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 将指定文件复制到指定路径下
        /// </summary>
        /// <param name="originFile">源文件路径(含文件名)</param>
        /// <param name="targetFile">目标文件路径(含文件名)</param>
        /// <returns>是否操作成功，成功true，错误false</returns>
        public static bool CopyFile(string originFile, string targetFile)
        {
            try
            {
                File.Copy(originFile, targetFile, true);
                return true;
            }
            catch
            {
            }
            return false;
        }

        /// <summary>
        /// 将一个文件夹下的文件全部复制到另一个文件夹下
        /// </summary>
        /// <param name="originFolder">源路径</param>
        /// <param name="targetFolder">目标路径</param>
        public static void CopyFiles(string originFolder, string targetFolder)
        {
            try
            {
                if (!Directory.Exists(targetFolder))
                {
                    _ = Directory.CreateDirectory(targetFolder);
                }
                List<string> files = SearchFilePaths(originFolder);
                foreach (string item in files)
                {
                    string newFile = targetFolder + "\\" + item.Substring(item.LastIndexOf("\\") + 1);
                    File.Copy(item, newFile, true);
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件内容</returns>
        public static string ReadFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return string.Empty;
            }
            try
            {
                string strContent = File.ReadAllText(filePath, Encoding.UTF8);
                return strContent;
            }
            catch
            {
            }
            return string.Empty;
        }

        /// <summary>
        /// 将字符串写入文件中 true追加内容,false覆盖
        /// </summary>
        /// <param name="flag">true追加内容,false覆盖</param>
        /// <param name="strContent">内容</param>
        /// <param name="filePath">文件路径</param>
        /// <returns>是否操作成功，成功true，错误false</returns>
        public static bool WriteFile(bool flag, string strContent, string filePath)
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    using (var file = File.Create(filePath))
                    { }
                }
                if (flag)
                {
                    File.AppendAllText(filePath, strContent, Encoding.UTF8);
                }
                else
                {
                    File.WriteAllText(filePath, strContent, Encoding.UTF8);
                }
                return true;
            }
            catch
            {
            }
            return false;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>是否操作成功，成功true，错误false</returns>
        public static bool DeleteFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return false;
            }
            try
            {
                File.Delete(filePath);
                return true;
            }
            catch
            {
            }
            return false;
        }

    }
}
